package com.fanjia.demologin.web;

import com.fanjia.demologin.util.ResultUtil;
import com.github.qcloudsms.httpclient.HTTPException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.sql.SQLIntegrityConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(RuntimeException.class)
    public ResultUtil doHandleRuntimeException(RuntimeException e) {
        e.printStackTrace();//控制台输出
        log.error("exception msg {}" + e.getMessage());
        if (e instanceof DuplicateKeyException) {
            return ResultUtil.error("手机号重复");
        } else {
            return ResultUtil.error("系统维护中");
        }

    }


    @ExceptionHandler(Exception.class)
    public ResultUtil doHandleException(Exception e) {
        e.printStackTrace();//控制台输出
        log.error("exception msg {}" + e.getMessage());
        if (e instanceof IOException) {
            return ResultUtil.error("发送验证码失败");
        }else if (e instanceof HTTPException){
            return ResultUtil.error("发送验证码失败");
        }
        else {
            return ResultUtil.error("系统维护中");
        }

    }





}